package com.example.demo.controller;

import com.example.demo.pojo.Page;
import com.example.demo.pojo.User;
import com.example.demo.pojo.UserRequest;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @TODO totalPage过多的page标签分布，import和export的实现，切换每页显示个数的实现，page搜索功能的实现
     * @param page
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String usersList(@RequestParam(required = false) Integer page,
                            @RequestParam(required = false) String keywords,
                            Model model,
                            HttpServletRequest request) {
        if (keywords == null && page == null) {
            keywords = "";
            page = 1;
        } else if (keywords == null) {
            keywords = (String) request.getSession().getAttribute("keyword");
        } else if (page == null) {
                page = 1;
        }
        request.getSession().removeAttribute("keyword");
        request.getSession().setAttribute("keyword", keywords);
        System.out.println(keywords);
        Page pageItem = userService.searchUsers(page, keywords);
        model.addAttribute("pages", pageItem);
        model.addAttribute("keywords", keywords);
        return "/users";
    }

    /**
     * @TODO user信息不全时的验证
     * @param user
     * @return
     */
    @RequestMapping("/toAddPage")
    public String toAddPage(User user) {

        return "/user";
    }

    @RequestMapping("/add")
    public String addUser(User user, Model model) {

        boolean b = userService.addUser(user);
        String msg;
        if (b) {
            return "redirect:/user/list?page=1";
        } else {
            msg = "添加失败";
            model.addAttribute("Messages", msg);
            return "/user";
        }
    }

    /**
     * @TODO user信息不全时的验证
     * @param u_id
     * @param model
     * @return
     */
    @RequestMapping("/toEditPage")
    public String toEditPage(@RequestParam Integer u_id, Model model) {

        User user = userService.findUserById(u_id);
        model.addAttribute("user", user);
        return "/user-editor";
    }

    @RequestMapping("/edit")
    public String edit(User user, Model model) {

        String msg;
        boolean b = userService.editUser(user);
        if (b) {
            return "redirect:/user/list?page=1";
        } else {
            msg = "修改失败";
            model.addAttribute("Messages", msg);
            return "/user-editor";
        }
    }

    @RequestMapping("/fromDelete")
    public String redirectFromDelete() {

        return "/delete-page";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam Integer u_id,
                         @RequestBody String string) {
        userService.deleteUser(u_id);
        System.out.println(string);
        return "redirect:/user/fromDelete";
    }

    @RequestMapping("/active")
    public String activeUser(@RequestParam(defaultValue = "1") int page,
                             Model model) {
        Page page1 = userService.findActiveUser(page);
        model.addAttribute("pages", page1);
        return "/active-user";
    }


    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String findUser(UserRequest userRequest, Model model) {
        User user = userService.findUserByName(userRequest.getU_name());
        model.addAttribute("username", userRequest.getU_name());
        return "/home";
    }

    @PostMapping(value = "/receive"
            , consumes = MediaType.APPLICATION_JSON_VALUE
            , produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<UserRequest> receiveRequestBody(@RequestBody UserRequest userRequest) {
        return new ResponseEntity<>(userRequest, HttpStatus.OK);
    }

    @PostMapping(value = "/{userId}/{username}")
    public String receivePathVariable(@PathVariable("userId") Integer id
            , @PathVariable("username") String name
            , Model model) {
        model.addAttribute("id", id);
        model.addAttribute("username", name);
        return "/home";
    }

}
